package com.tjbank.cssys.web.util;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class DateUtils {
    /**
     * 获取批次号
     *
     * @return
     */
    public static String getBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMMdd");
        String format = yyyyMM.format(date);
        String stringDate = format.substring(6, 8);
        if (Integer.valueOf(stringDate) < 15) {
            String batch = format.substring(0, 6)+"01" ;
            return batch;
        }
        String batch = format.substring(0, 6) + "15";
        return batch;
    }


    /**
     * 获取年月模糊查询使用
     * @return
     */
    public String getLikeBatchNumber() {
        Date date = new Date();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        String format = "%"+yyyyMM.format(date)+"%";
        return format;
    }

    public static String setBatchNumber(String batchNumber){
        String value = batchNumber.substring(4, 6);
        String methodValue = null;
        String year=null;
        if ("12".equals(value)) {
           year= String.valueOf(Integer.valueOf(batchNumber.substring(0, 4))+1);
            methodValue = "01";
        } else {
            if (Integer.valueOf(value) + 1 < 10) {
                methodValue = "0" + String.valueOf(Integer.valueOf(value) + 1);
            } else {
                methodValue = String.valueOf(Integer.valueOf(value) + 1);
            }
             year=batchNumber.substring(0, 4);
        }
        String batchValue = year + methodValue + batchNumber.substring(6, 8);
        return batchValue;
    }
    /***
     * 获取下一个月的批次号
     * @param date
     * @return
     */
    public static String  getlasetBatchNumber(String date){
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)+1);
        String result = new SimpleDateFormat("yyyyMM").format(instance.getTime());
        return result+date;
    }

    /**
     * 获取前一个批次号
     * @return
     */
    public static String getPreBatchNumber(){
        Calendar instance = Calendar.getInstance();
        Date today = instance.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(today);
        String stringDate = format.substring(6, 8);
        String batchStr = "";
        if(Integer.valueOf(stringDate) < 15){
            //取上个月的15号
            instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)-1);
            batchStr = new SimpleDateFormat("yyyyMM").format(instance.getTime()) + "15";
        }else{
            //取本月的1号
            batchStr = format.substring(0, 6) + "01";
        }
        return batchStr;
    }




}
